package com.qf.controller;

import com.qf.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生管理后台地址
 */
@Controller
@RequestMapping("/student")
public class StudentController {
    private static List<Student> list = new ArrayList<>();
    @RequestMapping("/findAll")
    public String findAllStudent(Model model, HttpServletRequest request, HttpSession httpSession){
        //将list存储在Model模型对象中
        model.addAttribute("list",list);
        return "student_list";
    }

    /**
     * 前端:xxxx/del?id=xxx
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public String delStudent(Integer id){
        System.out.println(id);
        Student student = list.remove(id - 1);
        System.out.println(student);
        //springmvc:字符串:redirect:重定向路径
        return "redirect:/student/findAll";
    }
    @RequestMapping("/find/{id}")
    public String findudentById(@PathVariable("id")Integer id,Model model){
        System.out.println(id);
        Student student = list.get(id);
        //设置模型数据
        model.addAttribute("student",student);
        return "student";
    }
    //无参构造给List集合添加数据
    public StudentController(){
        for (int x=0;x<10;x++){
            Student s = new Student();
            s.setId(1+x);
            s.setName("小芳"+x);
            s.setAge(14+x);
            list.add(s);
        }
    }
}
